<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2016 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 流年 <liu21st@gmail.com>
// +----------------------------------------------------------------------
/**
     * 模拟post进行url请求
     * @param string $url
     * @param string $param
     */


    use org\alisms\SignatureHelper;
    function request_post($url = '', $param = '') {
        if (empty($url) || empty($param)) {
            return false;
        }

        $postUrl = $url;
        $curlPost = $param;
        $ch = curl_init();//初始化curl
        curl_setopt($ch, CURLOPT_URL,$postUrl);//抓取指定网页
        curl_setopt($ch, CURLOPT_HEADER, 0);//设置header
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//要求结果为字符串且输出到屏幕上
        curl_setopt($ch, CURLOPT_POST, 1);//post提交方式
        curl_setopt($ch, CURLOPT_POSTFIELDS, $curlPost);
        $data = curl_exec($ch);//运行curl
        curl_close($ch);

        return $data;
    }
// 应用公共文件
    function https_post($url,$data=null){
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
        curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)');
        @curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
        curl_setopt($ch, CURLOPT_AUTOREFERER, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        $tmpInfo = curl_exec($ch);
        curl_close($ch);
        return $tmpInfo;
        $tmpInfo1 = (array)json_decode($tmpInfo);
        return $tmpInfo1;
    }

    function httpGet($url){
        $curl = curl_init();
        curl_setopt($curl,CURLOPT_RETURNTRANSFER,true);
        curl_setopt($curl,CURLOPT_TIMEOUT,500);
        curl_setopt($curl,CURLOPT_SSL_VERIFYPEER,false);
        curl_setopt($curl,CURLOPT_SSL_VERIFYHOST,false);
        curl_setopt($curl,CURLOPT_URL,$url);
        $res = curl_exec($curl);
        curl_close($curl);
        return $res;
    }

    //发送模板消息
    //$type    1预约成功 2预约失败 3支付成功 4支付失败
    function send_notice_pay($content,$type){


       //var_dump($content);die;
    $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$content['appid']."&secret=".$content['appsecret'];
    $con = httpGet($url);

    $ret = json_decode(htmlspecialchars_decode($con), true);

    if(isset($ret['access_token'])){

            if ($type == 1) {
                $templateData = yycg($content);
            }elseif($type == 2){
                $templateData = yysb($content);
            }elseif($type == 3){
                $templateData = gmcg($content);
            }elseif($type == 4){
                $templateData = gmsb($content);
            }




        $res = https_post('https://api.weixin.qq.com/cgi-bin/message/wxopen/template/send?access_token='.$ret["access_token"],$templateData);
        //var_dump($res);die;
        return $res;
         //var_dump($res);die;

    }
}

//购买成功模板
function gmcg($content){
    $templateData['keyword1']['value'] = $content['danhao'];
    $templateData['keyword2']['value'] = $content['time'];
    $templateData['keyword2']['color'] = '#d81e06';
    $templateData['keyword3']['value'] = $content['shopname'];
    $templateData['keyword4']['value'] = $content['money'].'元';
    $templateData['keyword5']['value'] = '已受理';
    $templateData['keyword6']['value'] = '微信支付';
    $templateData['keyword7']['value'] = '您的商品很快就飞奔到您手上咯！';
    $data['touser'] = $content['openid'];
    $data['template_id'] = $content['blade_id'];
    $data['page'] = 'pages/index/index';//用户点击模板消息后的跳转页面
    $data['form_id'] = $content['form_id'];
    $data['data'] = $templateData;
    return json_encode($data);
}

//购买失败模板
function gmsb($content){

    $templateData['keyword1']['value'] = '未支付';
    $templateData['keyword1']['color'] = '#d81e06';
    $templateData['keyword2']['value'] = $content['xiadantime'];
    $templateData['keyword3']['value'] = '若有疑问，请咨询客服。';
    $templateData['keyword4']['value'] = $content['zhifutime'];
    $templateData['keyword5']['value'] = '待付款';
    $data['touser'] = $content['openid'];
    $data['template_id'] = $content['gmsb_id'];
    $data['page'] = 'pages/index/index';//用户点击模板消息后的跳转页面
    $data['form_id'] = $content['form_id'];
    $data['data'] = $templateData;
    return json_encode($data);
}
//预约成功模板
function yycg($content){
    $templateData['keyword1']['value'] = $content['yyname'];
    $templateData['keyword2']['value'] = $content['yytel'];
    $templateData['keyword3']['value'] = $content['yytime'];
    $templateData['keyword3']['color'] = '#d81e06';
    $templateData['keyword4']['value'] = $content['tishi'];
    $templateData['keyword5']['value'] = $content['dangqiantime'];
    $data['touser'] = $content['openid'];
    $data['template_id'] = $content['yycg_id'];
    $data['page'] = 'pages/index/index';//用户点击模板消息后的跳转页面
    $data['form_id'] = $content['form_id'];
    $data['data'] = $templateData;
    return json_encode($data);
}

//预约失败模板
function yysb($content){
    $templateData['keyword1']['value'] = $content['dianming'];
    $templateData['keyword2']['value'] = $content['jujue'];

    $templateData['keyword2']['color'] = '#d81e06';
    $templateData['keyword3']['value'] = $content['time'];
    $templateData['keyword4']['value'] = date('Y年m月d日');
    $data['touser'] = $content['openid'];
    $data['template_id'] = $content['yysb_id'];
    $data['page'] = 'pages/index/index';//用户点击模板消息后的跳转页面
    $data['form_id'] = $content['form_id'];
    $data['data'] = $templateData;
    return json_encode($data);
}


/**
 * 发送短信
 */
function sendSms($phone,$code,$SignName,$TemplateCode,$accessKeyId,$accessKeySecret) {
    $params = array ();

    // *** 需用户填写部分 ***

    // fixme 必填: 请参阅 https://ak-console.aliyun.com/ 取得您的AK信息


    // fixme 必填: 短信接收号码
    $params["PhoneNumbers"] = "$phone";

    // fixme 必填: 短信签名，应严格按"签名名称"填写，请参考: https://dysms.console.aliyun.com/dysms.htm#/develop/sign
    $params["SignName"] = "$SignName";

    // fixme 必填: 短信模板Code，应严格按"模板CODE"填写, 请参考: https://dysms.console.aliyun.com/dysms.htm#/develop/template
    $params["TemplateCode"] = "$TemplateCode";

    // fixme 可选: 设置模板参数, 假如模板中存在变量需要替换则为必填项
    $params['TemplateParam'] = Array (
        "code" => "$code"
    );

    // fixme 可选: 设置发送短信流水号
    $params['OutId'] = "12345";

    // fixme 可选: 上行短信扩展码, 扩展码字段控制在7位或以下，无特殊需求用户请忽略此字段
    $params['SmsUpExtendCode'] = "1234567";


    // *** 需用户填写部分结束, 以下代码若无必要无需更改 ***
    if(!empty($params["TemplateParam"]) && is_array($params["TemplateParam"])) {
        $params["TemplateParam"] = json_encode($params["TemplateParam"], JSON_UNESCAPED_UNICODE);
    }

    // 初始化SignatureHelper实例用于设置参数，签名以及发送请求
    $helper = new SignatureHelper();

    // 此处可能会抛出异常，注意catch
    $content = $helper->request(
        $accessKeyId,
        $accessKeySecret,
        "dysmsapi.aliyuncs.com",
        array_merge($params, array(
            "RegionId" => "cn-hangzhou",
            "Action" => "SendSms",
            "Version" => "2017-05-25",
        ))
    );

    return $content;
}



